using DataBase;
using DataBase.DBmodels;
using DataBase.Utils;
using GreenXi.Help;
using GreenXi.Help.Dam;
using GreenXi.Help.Plc;
using GreenXi.Help.PLC;
using GreenXi.Model;
using GreenXi.Pages.Navig;
using GreenXi.Pages.Power;
using GreenXi.ViewModel.Utils;
using Helpers;
using Microsoft.EntityFrameworkCore;
using NModbus;
using PressSystem_Can.Help.Can;
using S7.Net;
using SuperSimpleTcp;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.IO.Ports;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using Util;
using ZedGraph;
using static PressSystem_Can.Help.Can.CanHelper;
using Craft = GreenXi.Pages.Setting.Craft;
using MessageBox = System.Windows.MessageBox;
using MessageBoxOptions = System.Windows.MessageBoxOptions;
namespace GreenXi
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : System.Windows.Application
    {
        System.Threading.Mutex mutex;

        public App()
        {
         this.Startup += new StartupEventHandler(App_Startup);
          
        }

        void App_Startup(object sender, StartupEventArgs e)
        {
            bool ret;
           
            mutex = new System.Threading.Mutex(true, "GreenXi", out ret);

            if (!ret)
            {
                MessageBox.Show("已有一个程序实例运行");
                Environment.Exit(0);
            }
        }

        #region 配置文件相关
        protected override async void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            #region 曲线合格测试

            //var decisions = MyDbContext.Instance.GetDecision("1");
            //var List = new PointPairList();
            //for(int i=0; i < 100; i++)
            //{
            //    List.Add(new PointPair(i, i));
            //}

            //var aa= SmoothChart.SmoothChartResult(List,decisions);

            #endregion

            #region CSV写入读取测试
            //CSVUtil.WriteToCsv(new()
            //{
            //    CurrentPosition = "1",
            //    CurrentPressure = "2",
            //    JoinTime = DateTime.Now,
            //}, new()
            //{
            //    BorderType = "test"
            //}, "Position", "a.csv");

            //CSVUtil.WriteToCsv(new()
            //{
            //    CurrentPosition = "1",
            //    CurrentPressure = "2",
            //    JoinTime = DateTime.Now,
            //}, new()
            //{
            //    BorderType = "test"
            //}, "Position", "a.csv");
            //CSVUtil csv = new();
            //List<Util.Model.CSVData> a;
            //List<Util.Model.Border> b;

            //csv.ReadFromCsv("", "20231030125920-0.csv", out a, out b);
            //csv.ReadFromCsv("", "20231030125920-1.csv", out a, out b);
            //csv.ReadFromCsv("", "20231030125920-2.csv", out a, out b);
            //csv.ReadFromCsv("", "20231030125920-3.csv", out a, out b);

            //// 创建一个打开文件对话框实例
            //Microsoft.Win32.OpenFileDialog openFileDialog = new();

            //// 设置对话框的标题和筛选条件（可选）
            //openFileDialog.Title = "选择文件";
            //openFileDialog.Filter = "CSV文件 (*.CSV)|*.CSV";

            //// 显示对话框并获取用户的操作结果
            //bool? result = openFileDialog.ShowDialog();

            //if (result == true)
            //{
            //    // 获取所选文件的完整路径
            //    string selectedFilePath = openFileDialog.FileName;

            //    // 获取文件所在目录的路径
            //    string directoryPath = System.IO.Path.GetDirectoryName(selectedFilePath);

            //}

            var stop = 0;
            #endregion

           //var aa= ModBus.GetModel("01 03 20 9F C1 50 00 1A 0E 58 00 0A 02 0E 1D 00 00 00 68 0D DB 7C 00 00 0A 00 33 06 03 2E 09 33 09 35 09 2F 00 13 00 14 00 D0 00 C7 00 26 00 00 00 00 00 23 FF EB 00 00 00 00 FF F2 00 2C 00 00 00 00 00 26 FC 90 03 E8 FD B8 FC 64 11 3B");
            

            #region 密钥相关
            string connectStr = "Server=localhost;Port=3306;Database=haina;Uid=root;Pwd=123456;Charset=utf8";
            string backupStr = "mysqldump.exe --user=root --password=123456 --host=localhost --protocol=tcp --port=3306 ";
            string encrypt1 = Security.Encrypt(connectStr);
            string decrypt1 = Security.Decrypt(encrypt1);

            string encrypt2 = Security.Encrypt(backupStr);
            string decrypt2 = Security.Decrypt(encrypt2);
            #endregion

            #region 配置检测
            //var backupIntervalDay = MyDbContext.GetSettingByDb<int>("BackupIntervalDay");

            //var backupIntervalTime = TimeSpan.Parse(MyDbContext.GetSettingByDb<string>("BackupIntervalTime"));

            //string cronStr = Utl.CronConverter.GetCornString(backupIntervalDay, backupIntervalTime);
            #endregion

            #region ModBus通信测试模块
            //ModBus.ConnectServoMotor();
            //var b = ModBus.GetPosition();

            #endregion

            #region 数据初始化，设备连接

  

         
            ModBus.Init();

            Helpers.Logger.Instance.Infomation("打开程序");
            //打开定时任务
            ScheduledTasks.RegisterJobEntrance();
            #endregion


        }

        protected override void OnExit(ExitEventArgs e)
        {

            base.OnExit(e);
        }
        #endregion

        #region 保存用户登录信息
        public static class CurrentUser
        {
            public static string? UserName { get; set; }
            public static int UserId { get; set; }
            public static int RoleId { get; set; }
            public static int RoleLevel { get; set; }

            //获取用户信息列表
            public static List<DataBase.User> GetUserInfoByCurrentUserWeight()
            {
                //var currentUser = GetUser(UserName); // 假设有一个名为 CurrentUser 的变量，表示当前登录用户
                var users = MyDbContext.Instance.Users
                    .Where(x => x.IsEnabled == 1)
                    .ToList();
                //if (currentUser != null)
                //{
                //    users.Add(currentUser); // 将当前登录用户加入列表
                //}
                return users;
            }

            //获取当前用户信息
            public static DataBase.User? GetUser(string UserName)
            {
                var user = MyDbContext.Instance.Users.FirstOrDefault(x => x.UserName == UserName);
                return user;
            }

            //获取权限列表
            public static List<string> GetRoleGroupList()
            {
                var roleList = MyDbContext.Instance.RolePermissions.Where(x => x.RoleLevel >= RoleLevel).Select(x => x.PermissionCode).ToList();
                return roleList;
            }

            public static void Clean()
            {
                UserName = null;
                UserId = -1;
                RoleId = -1;
                RoleLevel = 999;
            }
        }
        #endregion
    }

    public static class GlobalVariables
    {
        public static int MultiCard_Catch_count = 0;
        public static bool IsDebugMode = MyDbContext.GetSettingByDb<bool>("IsDebugMode", false, "是否为调试模式");

        #region 获取数据的相关静态方法
        //获取压力
        public static double GetPressure(int nAxisNum)
        {
            var res = Card_Pressure1;
            if (nAxisNum == 2)
            {
                res = Card_Pressure2;
            }
            return res;
        }

        //获取位移
        public static double GetDisplacement(int nAxisNum)
        {
            var res = Card_Displacement1;
            if (nAxisNum == 2)
            {
                res = Card_Displacement2;
            }
            return res;
        }

        //获取压机模式 0是手动 1是自动
        public static int GetCard_Mode(int nAxisNum)
        {
            var res = Card_Mode1;
            if (nAxisNum == 2)
            {
                res = Card_Mode2;
            }
            return res;
        }

        //设置压机是否正在执行
        public static void SetIsRunning(int nAxisNum, bool isRunning)
        {
            if (nAxisNum == 1)
            {
                IsRunning1 = isRunning;
            }
            if (nAxisNum == 2)
            {
                IsRunning2 = isRunning;
            }
        }

        //设置PLC状态
        public static void SetPLCStatus(int nAxisNum, int status)
        {
           
        }

        //设置PLC状态
        public static void SetMES_State(int nAxisNum, int status)
        {
            PLC1 pLC = new();
            pLC.SetPLC($"Adr_MES_State{nAxisNum}", (short)status);
        }

        #endregion

        #region 心跳与连接状态
        //PLC心跳
        private static bool _PLCHeartBeat = false;
        public static bool PLCHeartBeat
        {
            get { return _PLCHeartBeat; }
            set
            {
                _PLCHeartBeat = value;
                if (value)
                {
                    MainWindow.Instance.PLCHeartBeatColor = "Green";
                }
                else
                {
                    MainWindow.Instance.PLCHeartBeatColor = "Gray";
                }
            }
        }


        #region 扫码枪相关
        public static string OP10PortName = MyDbContext.GetSettingByDb<string>("QRPortName1");

        public static string OP10BaudRate = MyDbContext.GetSettingByDb<string>("QRBaudRate1");

        public static SerialPort RQServoMotor { get; set; } = new SerialPort();
        
        #endregion

        //MES心跳
        public static bool MES_HeartBeat = false;

        // 当前模式 0:无模式 1：手动模式 2：自动模式
        private static int currentMode = 0;
        public static int CurrentMode
        {
            get { return currentMode; }
            set { currentMode = value; }
        }

        // 运动控制卡连接状态
        private static bool _IsMotionControlCard1Connected = false;
        public static bool IsMotionControlCard1Connected
        {
            get { return _IsMotionControlCard1Connected; }
            set
            {
                _IsMotionControlCard1Connected = value;
                if (value)
                {
                    // 当连接状态为 true 时，设置对应的视图模型属性为 "Green"
                    MainWindow.Instance.MotionControlCard1Color = "Green";
                }
                else
                {
                    // 当连接状态为 false 时，设置对应的视图模型属性为 "Gray"
                    MainWindow.Instance.MotionControlCard1Color = "Gray";
                }
            }
        }

        // 运动控制卡连接状态
        private static bool _IsMotionControlCard2Connected = false;
        public static bool IsMotionControlCard2Connected
        {
            get { return _IsMotionControlCard2Connected; }
            set
            {
                _IsMotionControlCard2Connected = value;
                if (value)
                {
                    // 当连接状态为 true 时，设置对应的视图模型属性为 "Green"
                    MainWindow.Instance.MotionControlCard2Color = "Green";
                }
                else
                {
                    // 当连接状态为 false 时，设置对应的视图模型属性为 "Gray"
                    MainWindow.Instance.MotionControlCard2Color = "Gray";
                }
            }
        }

        // 压力传感器连接状态
        private static bool _IsPressureSensor1Connected = false;
        public static bool IsPressureSensor1Connected
        {
            get { return _IsPressureSensor1Connected; }
            set
            {
                _IsPressureSensor1Connected = value;
                if (value)
                {
                    // 当连接状态为 true 时，设置对应的视图模型属性为 "Green"
                    MainWindow.Instance.PressureSensor1Color = "Green";
                }
                else
                {
                    // 当连接状态为 false 时，设置对应的视图模型属性为 "Gray"
                    MainWindow.Instance.PressureSensor1Color = "Gray";
                }
            }
        }

        // 压力传感器连接状态
        private static bool _IsPressureSensor2Connected = false;
        public static bool IsPressureSensor2Connected
        {
            get { return _IsPressureSensor2Connected; }
            set
            {
                _IsPressureSensor2Connected = value;
                if (value)
                {
                    // 当连接状态为 true 时，设置对应的视图模型属性为 "Green"
                    MainWindow.Instance.PressureSensor2Color = "Green";
                }
                else
                {
                    // 当连接状态为 false 时，设置对应的视图模型属性为 "Gray"
                    MainWindow.Instance.PressureSensor2Color = "Gray";
                }
            }
        }

        // 伺服电机连接状态
        private static bool _IsServoMotor1Connected = false;
        public static bool IsServoMotor1Connected
        {
            get { return _IsServoMotor1Connected; }
            set
            {
                _IsServoMotor1Connected = value;
                if (value)
                {
                    // 当连接状态为 true 时，设置对应的视图模型属性为 "Green"
                    MainWindow.Instance.ServoMotor1Color = "Green";
                }
                else
                {
                    // 当连接状态为 false 时，设置对应的视图模型属性为 "Gray"
                    MainWindow.Instance.ServoMotor1Color = "Gray";
                }
            }
        }

        // 伺服电机连接状态
        private static bool _IsServoMotor2Connected = false;
        public static bool IsServoMotor2Connected
        {
            get { return _IsServoMotor2Connected; }
            set
            {
                _IsServoMotor2Connected = value;
                if (value)
                {
                    // 当连接状态为 true 时，设置对应的视图模型属性为 "Green"
                    MainWindow.Instance.ServoMotor2Color = "Green";
                }
                else
                {
                    // 当连接状态为 false 时，设置对应的视图模型属性为 "Gray"
                    MainWindow.Instance.ServoMotor2Color = "Gray";
                }
            }
        }

        // 报警状态
        private static int _CallPolice = 0;
        /// <summary>
        /// 10运动控制卡断开
        /// 
        /// 急停的时候的操作
        /// 1.需要给运动控制卡发停止指令
        /// 2.检查一下是否有进行中的自动运行程序,杀死自动运行程序
        /// </summary>
        public static int CallPolice
        {
            get { return _CallPolice; }
            set
            {
                _CallPolice = value;
                //PLC报警屏蔽调试用
                if (value == 0)
                {
                    MainWindow.Instance.LoginColor = "Green";
                    if (PressureCurve.Instance != null)
                    {
                        PressureCurve.Instance.PoliceStr = PoliceStr;
                        PressureCurve.Instance.IsPolice = false;
                        PressureCurve.Instance.ListError.Clear();
                        ErrorMenuList.Clear();
                    }
                }
                else
                {
                   
                    if (PressureCurve.Instance != null)
                    {
                        PressureCurve.Instance.PoliceStr = PoliceStr;
                        PressureCurve.Instance.IsPolice = true;
                        var error = new Model.ErrorMenu() { Time = DateTime.Now, ErrorCode = CallPolice.ToString(), ErrorMsg = PoliceStr };
                        ErrorMenuList.Add(error);
                        MyDbContext.SetLog(PoliceStr, "");
                        MainWindow.Instance.LoginColor = "Red";
                        PressureCurve.Instance.ListError.Add(error);
                       
                    }
                   
                  
                   
                }
            }
        }
        #endregion

        public static  List<ErrorMenu> ErrorMenuList=new List<ErrorMenu>();

    #region 数据库中的数据

        #region 系统相关
    public static string SystemName { get; set; } = MyDbContext.GetSettingByDb("SystemName", "能源采集系统", "系统名称");

        public static string PoliceStr { get; set; }
        //当前启用压机数量
        private static double compressorsNumber;
        public static double CompressorsNumber
        {
            get { return compressorsNumber; }
            set
            {
                compressorsNumber = value;
                if (compressorsNumber == 2)
                {
                    SystemSettingsView.Instance.DisplayVisibility = "Visible";
                    MainWindow.Instance.PressDisplayVisibility = "Visible";
                    Craft.Instance.IsVisibility = "Visible";
                 
                }
                else
                {
                    SystemSettingsView.Instance.DisplayVisibility = "Collapsed";
                    MainWindow.Instance.PressDisplayVisibility = "Collapsed";
                    Craft.Instance.IsVisibility = "Collapsed";
               
                }
            }
        }
        #endregion

        #region PLC相关
        public static readonly Plc plc = null;
        public static readonly Plc plc2 = null;


        public static int TCPPort { get; set; } = MyDbContext.GetSettingByDb("TCPPort", 8080, "TCP端口号");

        public static string DataIP = MyDbContext.GetSettingByDb("DataIP", "192.168.23.10", "数据库IP地址");

        public static int DataPort = MyDbContext.GetSettingByDb("DataPort", 102, "数据库端口");

        public static string DataUser = MyDbContext.GetSettingByDb<string>("DataUser", "0", "数据库用户名");

        public static string DataPassword = MyDbContext.GetSettingByDb<string>("DataPassword", "0", "数据库密码");

        
        #endregion

        

        #region 伺服电机相关
       

        public static System.Net.Sockets.Socket server_socketListen;
        public static System.Net.Sockets.Socket socket_commu;

        public static SimpleTcpServer server;

        public static string ServoMotorIp1 = MyDbContext.GetSettingByDb("ServoMotorIp1", "192.168.0.201", "ServerIp");


        #endregion

        

        #region 限制设置
        public static ObservableCollection<int> MaximumDisplacementList = MyDbContext.GetSettingListByDb("MaximumDisplacement", 180, "180", "");

        public static ObservableCollection<int> MinimumDisplacementList = MyDbContext.GetSettingListByDb("MinimumDisplacement", 2, "2", "");

        public static ObservableCollection<int> GreatestPressureList = MyDbContext.GetSettingListByDb("GreatestPressure", 20, "20", "");


     

        public static double Acceleration = MyDbContext.GetSettingByDb<double>("Acceleration");

        public static double Deceleration = MyDbContext.GetSettingByDb<double>("Deceleration");

        public static double SmallJogSpeed = MyDbContext.GetSettingByDb<double>("SmallJogSpeed");

        public static double BigJogSpeed = MyDbContext.GetSettingByDb<double>("BigJogSpeed");

        public static int MaximumDisplacement = MyDbContext.GetSettingByDb<int>("MaximumDisplacement");

        public static int MinimumDisplacement = MyDbContext.GetSettingByDb<int>("MinimumDisplacement");

        public static int GreatestPressure = MyDbContext.GetSettingByDb<int>("GreatestPressure");

        public static int MaximumSpeed = MyDbContext.GetSettingByDb<int>("MaximumSpeed");

        public static int ServoMotorSlaveAddress = MyDbContext.GetSettingByDb<int>("ServoMotorSlaveAddress",1000,"");
        



        public static double ChartXLowerLimit = MyDbContext.GetSettingByDb<double>("ChartXLowerLimit", 0, "图表显示X轴下限");

    

        #endregion

        #region 显示
        public static ObservableCollection<int> CompressorsNumberList = MyDbContext.GetSettingListByDb("CompressorsNumber", 1, "1,2", "");

        public static ObservableCollection<int> DecimalPlacesList = MyDbContext.GetSettingListByDb("DecimalPlaces", 0, "0", "小数位数");

        public static ObservableCollection<int> OriginList = MyDbContext.GetSettingListByDb("Origin", 4, "4,14", "");

        public static int Origin = MyDbContext.GetSettingByDb<int>("Origin");

        public static DateTime LastTime=DateTime.Now;

        #endregion

        #region 备份设置
        public static int BackupIntervalDay = MyDbContext.GetSettingByDb("BackupIntervalDay", 1, "备份的间隔日期（0-31）");

        public static TimeSpan BackupIntervalTime = TimeSpan.Parse(MyDbContext.GetSettingByDb("BackupIntervalTime", "00:00:00", "备份的时间"));
        #endregion


        #endregion

        #region 连接设备
        public static SerialPort ServoMotor1 { get; set; } = new SerialPort();

        public static SerialPort ServoMotor2 { get; set; } = new SerialPort();




        public static IModbusMaster ModbusSerialMaster1 { get; set; }

        #endregion

        #region CSV存储相关
        //当前要存储的CSV文件的名称
        public static string DataFileName { get; set; }

        //执行的程序名
        public static string ExecutedProgramName { get; set; }

        //上一次位移
        public static double LastPosition { get; set; }

        //当前位移
        public static double CurrentPosition { get; set; }

        //目标位移
        public static double TargetPosition { get; set; }

        //当前压力
        public static double CurrentPressure { get; set; }

        //目标压力
        public static double TargetPressure { get; set; }
        #endregion

        #region 实时数据和压装状态

        public static int[] IOParameter = new int[8];
        //压机实时的位移数


        #region 线程控制开关
        //是否已经强制关闭压机了，如果是，就不要执行后续的压装程序了


        private static bool _IsForcedStop = false;
        public static bool IsForcedStop
        {
            get { return _IsForcedStop; }
            set
            {
                _IsForcedStop = value;
                if (value)
                {
                    MainWindow.Instance.LoginColor = "Red";
                }
            }
        }

        //对应压机是否到了保护力
        private static bool _IsProtectStop1 = false;
        public static bool IsProtectStop1
        {
            get { return _IsProtectStop1; }
            set { _IsProtectStop1 = value; }
        }

        //对应压机是否到了保护力
        private static bool _IsProtectStop2 = false;
        public static bool IsProtectStop2
        {
            get { return _IsProtectStop2; }
            set { _IsProtectStop2 = value; }
        }

		//保护位移1
		private static bool _IsProtectDisplacement1 = false;
		public static bool IsProtectDisplacement1
		{
			get { return _IsProtectDisplacement1; }
			set { _IsProtectDisplacement1 = value; }
		}

		//保护位移2

		private static bool _IsProtectDisplacement2 = false;
		public static bool IsProtectDisplacement2
		{
			get { return _IsProtectDisplacement2; }
			set { _IsProtectDisplacement2 = value; }
		}

		//手动执行压装程序是否暂停
		private static bool _IsPrcStop1 = false;
        public static bool IsPrcStop1
        {
            get { return _IsPrcStop1; }
            set { _IsPrcStop1 = value; }
        }
        //手动执行压装程序是否暂停
        public static bool _IsPrcStop2 = false;
        public static bool IsPrcStop2
        {
            get { return _IsPrcStop2; }
            set { _IsPrcStop2 = value; }
        }

        //伺服是否报警，如果报警则不允许写入plc状态
        public static bool IsServoAlarm { get; set; } = false;

        //是否丢失位移数据，如果是则报警
        public static bool IsLostDisplacementData { get; set; } = false;


        //压机1是否正在压装
        public static bool IsRunning1 { get; set; } = false;

        //压机2是否正在压装
        public static bool IsRunning2 { get; set; } = false;

        //压机1是否正在压装
        public static bool IsWaiting1 { get; set; } = false;

        //压机2是否正在压装
        public static bool IsWaiting2 { get; set; } = false;

        #endregion

        

    #region 压机状态与模式
    //压机1Plc状态
    public static int Status = 1;

        //压机2Plc状态
        public static int Plc_Status2 = 0;


        //压机1模式 0是手动 1是自动
        private static int _Card_Mode1;

        public static int Card_Mode1
        {
            get { return _Card_Mode1; }
            set
            {
                _Card_Mode1 = value;
            }
        }

        //压机2模式 0是手动 1是自动
        public static int Card_Mode2 = 0;

        //压机1手动模式状态  false 停止 true压装中
        public static bool ManualModel_Status1 = false;

        //压机2手动模式状态  false 停止 true压装中
        public static bool ManualModel_Status2 = false;

        #endregion

        #region 压装程序名
        private static int _CylinderNo;
        public static int CylinderNo
        {
            get { return _CylinderNo; }
            set
            {
                _CylinderNo = value;
            }
        }

        //压机1压装程序名
        private static string _Cylinder1No;
        public static string Cylinder1No
        {
            get { return _Cylinder1No; }
            set
            {
                _Cylinder1No = value;
                if (PressureCurve.Instance != null)
                {
                    PressureCurve.Instance.PressingProcedure1 = value;
                }
            }
        }

        //压机2压装程序名
        private static string _Cylinder2No;
        public static string Cylinder2No
        {
            get { return _Cylinder2No; }
            set
            {
                _Cylinder2No = value;
                if (PressureCurve.Instance != null)
                {
                    PressureCurve.Instance.PressingProcedure2 = value;
                }
            }
        }
        #endregion

        #region 压机实时压力
        //压机1实时压力
        private static double _Card_Pressure1 = 0;
        public static double Card_Pressure1
        {
            get { return _Card_Pressure1; }
            set
            {
                _Card_Pressure1 = value;
                if (Manual.Instance != null)
                {
                    Manual.Instance.Pressure1 = value.ToString();
                }
                if (PressureCurve.Instance != null)
                {
                    PressureCurve.Instance.Pressure1 = value.ToString();
                }
            }
        }

        //压机2实时压力
        private static double _Card_Pressure2 = 0;
        public static double Card_Pressure2
        {
            get { return _Card_Pressure2; }
            set
            {
                _Card_Pressure2 = value;
                if (Manual.Instance != null)
                {
                    Manual.Instance.Pressure2 = value.ToString();
                }
                if (PressureCurve.Instance != null)
                {
                    PressureCurve.Instance.Pressure2 = value.ToString();
                }
            }
        }
        #endregion

        #region 压机实时位移
        //压机1实时位移
        private static double _Card_Displacement1 = 0;
        public static double Card_Displacement1
        {
            get { return _Card_Displacement1; }
            set
            {
                _Card_Displacement1 = value;
                if (Manual.Instance != null)
                {
                    Manual.Instance.Displacement1 = value.ToString();
                }
                if (PressureCurve.Instance != null)
                {
                    PressureCurve.Instance.Displacement1 = value.ToString();
                }
            }
        }

        //压机2实时位移
        private static double _Card_Displacement2 = 0;
        public static double Card_Displacement2
        {
            get { return _Card_Displacement2; }
            set
            {
                _Card_Displacement2 = value;
                if (Manual.Instance != null)
                {
                    Manual.Instance.Displacement2 = value.ToString();
                }
                if (PressureCurve.Instance != null)
                {
                    PressureCurve.Instance.Displacement2 = value.ToString();
                }
            }
        }
        #endregion

        

        #region 条码信息
        private static string _BarcodeInfo1 = "";
        public static string BarcodeInfo1
        {
            get { return _BarcodeInfo1; }
            set
            {
                _BarcodeInfo1 = value;
                PressureCurve.Instance.BarcodeInfo1 = value;
                if (Manual.Instance != null)
                {
                    Manual.Instance.BarcodeInfo1 = value;
                }
            }
        }

        private static string _BarcodeInfo2 = "";
        public static string BarcodeInfo2
        {
            get { return _BarcodeInfo2; }
            set
            {
                _BarcodeInfo2 = value;
                PressureCurve.Instance.BarcodeInfo2 = value;
                Manual.Instance.BarcodeInfo2 = value;
            }
        }
        #endregion

        #region 合格状态 默认状态0 合格1 不合格2
        private static int _IsQualified1 = 0;
        public static int IsQualified1
        {
            get { return _IsQualified1; }
            set
            {
                _IsQualified1 = value;
                switch (value)
                {
                    case 0:
                        PressureCurve.Instance.IsQualified1 = "Beige";
                        break;
                    case 1:
                        PressureCurve.Instance.IsQualified1 = "Green";
                        break;
                    case 2:
                        PressureCurve.Instance.IsQualified1 = "Red";
                        break;
                    default:
                        PressureCurve.Instance.IsQualified1 = "Beige";
                        break;
                }
            }
        }

        private static int _IsQualified2 = 0;
        public static int IsQualified2
        {
            get { return _IsQualified2; }
            set
            {
                _IsQualified2 = value;
                switch (value)
                {
                    case 0:
                        PressureCurve.Instance.IsQualified2 = "Beige";
                        break;
                    case 1:
                        PressureCurve.Instance.IsQualified2 = "Green";
                        break;
                    case 2:
                        PressureCurve.Instance.IsQualified2 = "Red";
                        break;
                    default:
                        PressureCurve.Instance.IsQualified2 = "Beige";
                        break;
                }
            }
        }
        #endregion

        /// <summary>
        /// 压力传感器是否打开
        /// </summary>
        public static bool IsPressureSensorOpen = false;

        #region 当前最大力和最大位移
        public static double? CurrentMaxDisplacement1;
        public static double? CurrentMaxDisplacement2;
        public static double? CurrentMaxPressure1;
        public static double? CurrentMaxPressure2;

        #endregion

        #endregion
        #region 压机实时曲线
        //压机2曲线
        public static PointPairList ListPress2 = new PointPairList();

        //压机1曲线
        public static PointPairList ListPress1 = new PointPairList();
        //压机1曲线


        public static List<PointPairList> ListPress = new List<PointPairList>();

        #endregion
        /// <summary>
        /// 电表实时数据
        /// </summary>

        private static List<DataBase.DBmodels.CraftView> _CraftViewList= MyDbContext.GetCraftViewByType(5);
        public static List<DataBase.DBmodels.CraftView> CraftViewList
        {
            get { return _CraftViewList; }
            set
            {
                _CraftViewList = value;
                if (PressureCurve.Instance != null)
                {
                    PressureCurve.Instance.CraftList = new(_CraftViewList.Where(X=>X.Type==3).Take(10));
                }
               
                   
            }
        }

        /// <summary>
        /// 温度实时数据
        /// </summary>

        private static List<DataBase.DBmodels.CraftView> _CraftViewList2 = MyDbContext.GetCraftViewByType(2);
        public static List<DataBase.DBmodels.CraftView> CraftViewList2
        {
            get { return _CraftViewList2; }
            set
            {
                _CraftViewList2 = value;
                if (PressureCurve.Instance != null)
                    PressureCurve.Instance.CraftList2 = new(_CraftViewList2.Take(4));
            }
        }

        /// <summary>
        /// 变压器温度实时数据
        /// </summary>

        private static List<DataBase.DBmodels.CraftView> _CraftViewList3 = MyDbContext.GetCraftViewByType(4);
        public static List<DataBase.DBmodels.CraftView> CraftViewList3
        {
            get { return _CraftViewList3; }
            set
            {
                _CraftViewList3 = value;
                if (PressureCurve.Instance != null)
                    PressureCurve.Instance.CraftList3 = new(_CraftViewList3.Take(6));
            }
        }


        private static List<DataBase.Gateway> _GatewayList = MyDbContext.Instance.GetGateway();
        public static List<DataBase.Gateway> GatewayList
        {
            get { return _GatewayList; }
            set
            {
                _GatewayList = value;
            }
        }
        //峰谷时间段
        private static List<DataBase.DBmodels.timeslot> _TimeslotList = MyDbContext.Instance.GetTimeslot();
        public static List<DataBase.DBmodels.timeslot> TimeslotList
        {
            get { return _TimeslotList; }
            set
            {
                _TimeslotList = value;
            }
        }
        #region 报警状态

        public enum CallPoliceStatus {

            超过电量总限位= 10,
            超过峰电量限位 = 20,
            超过谷电量限位 = 30,

            超过温度上限 = 50,

            网关连接异常 = 40,
        
            伺服报警_到达正限位 = 21,
       
            急停 = 19,
            伺服断开连接 = 51,
            
            位移达到限位 = 60,
            设置的行程超过限位 = 61,
            压力达到限位 = 70,
            PLC连接失败 = 80,
            传感器_到达负限位 = 90,
            传感器_到达正限位 = 100,
            伺服1_达到负极限=110,
            伺服1_达到正极限 = 120,
            伺服2_达到负极限 = 130,
            伺服2_达到正极限 = 140,

        }
        #endregion
    }
}
